/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConditionsTextEditorView.java
 *
 * Created on Feb 3, 2010, 4:38:47 PM
 * Author: barisergun
 */
package org.testis.gui.mvc.view.testdesign;

import org.testis.gui.mvc.controller.testdesign.ConditionTextEditorController;
import org.testis.gui.mvc.view.AbstractViewPanel;
import java.awt.Color;
import java.beans.PropertyChangeEvent;
import javax.swing.JOptionPane;

/**
 *
 * @author barisergun
 */
public class ConditionsTextEditorView extends javax.swing.JDialog implements AbstractViewPanel {

    /** Creates new form ConditionsTextEditorView */
    public ConditionsTextEditorView(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButtonApply = new javax.swing.JButton();
        jButtonCancel = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaCondtionText = new javax.swing.JTextArea();
        btnProfileVariable = new javax.swing.JButton();
        btnLanguageVariable = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextAreaEvaluationString = new javax.swing.JTextArea();
        jLabelConditionText = new javax.swing.JLabel();
        jLabelEvaluatedText = new javax.swing.JLabel();
        jTextFieldEvaluationResult = new javax.swing.JTextField();
        jLabelEvaluationResult = new javax.swing.JLabel();
        jLabelConditionId = new javax.swing.JLabel();
        jTextFieldConditionId = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setModal(true);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jButtonApply.setText("Apply");
        jButtonApply.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonApplyActionPerformed(evt);
            }
        });

        jButtonCancel.setText("Cancel");
        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelActionPerformed(evt);
            }
        });

        jTextAreaCondtionText.setColumns(20);
        jTextAreaCondtionText.setLineWrap(true);
        jTextAreaCondtionText.setRows(5);
        jTextAreaCondtionText.setWrapStyleWord(true);
        jTextAreaCondtionText.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextAreaCondtionTextFocusLost(evt);
            }
        });
        jScrollPane1.setViewportView(jTextAreaCondtionText);

        btnProfileVariable.setText("Profile Variable");
        btnProfileVariable.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnProfileVariableActionPerformed(evt);
            }
        });

        btnLanguageVariable.setText("Language Variable");
        btnLanguageVariable.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLanguageVariableActionPerformed(evt);
            }
        });

        jTextAreaEvaluationString.setColumns(20);
        jTextAreaEvaluationString.setEditable(false);
        jTextAreaEvaluationString.setLineWrap(true);
        jTextAreaEvaluationString.setRows(5);
        jTextAreaEvaluationString.setWrapStyleWord(true);
        jTextAreaEvaluationString.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextAreaEvaluationStringFocusGained(evt);
            }
        });
        jScrollPane2.setViewportView(jTextAreaEvaluationString);

        jLabelConditionText.setText("Enter condition text below");

        jLabelEvaluatedText.setText("Evaluated condition text");

        jTextFieldEvaluationResult.setEditable(false);

        jLabelEvaluationResult.setText("Evaluation result");

        jLabelConditionId.setText("Condition Id:");

        jTextFieldConditionId.setEditable(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(41, 41, 41)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelEvaluationResult)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jTextFieldEvaluationResult, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 213, Short.MAX_VALUE)
                        .addComponent(jButtonApply, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButtonCancel))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabelConditionId)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldConditionId, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabelEvaluatedText)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE))
                            .addComponent(jLabelConditionText))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(btnProfileVariable, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnLanguageVariable))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelConditionId)
                    .addComponent(jTextFieldConditionId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addComponent(jLabelConditionText)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(64, 64, 64)
                        .addComponent(btnProfileVariable, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnLanguageVariable))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(206, 206, 206)
                        .addComponent(jLabelEvaluatedText, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addComponent(jLabelEvaluationResult)
                .addGap(4, 4, 4)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldEvaluationResult, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonCancel)
                    .addComponent(jButtonApply))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnProfileVariableActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnProfileVariableActionPerformed

        ConditionTextEditorController.createInstance().operationProfileList();
}//GEN-LAST:event_btnProfileVariableActionPerformed

    private void btnLanguageVariableActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLanguageVariableActionPerformed
        ConditionTextEditorController.createInstance().operationLanguageList();
}//GEN-LAST:event_btnLanguageVariableActionPerformed

    private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButtonCancelActionPerformed

    private void jButtonApplyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonApplyActionPerformed
        // TODO add your handling code here:
        if (!jTextAreaCondtionText.getText().isEmpty()) {
            Integer condId = 0; // new condition string id is 0
            if (!jTextFieldConditionId.getText().isEmpty()) {
                condId = Integer.parseInt(jTextFieldConditionId.getText());
            }
            ConditionTextEditorController.createInstance().
                    operationApplyConditionText(condId, jTextAreaCondtionText.getText());

            this.dispose();
        } else {
            JOptionPane.showMessageDialog(this,
                    "Condition text can not be empty",
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jButtonApplyActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        // TODO add your handling code here:
        ConditionTextEditorController.createInstance().operationClose();
    }//GEN-LAST:event_formWindowClosed

    private void jTextAreaCondtionTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextAreaCondtionTextFocusLost
        ConditionTextEditorController.createInstance().
                operationSetConditionText(
                ((javax.swing.JTextArea) evt.getComponent()).getText());
    }//GEN-LAST:event_jTextAreaCondtionTextFocusLost

    private void jTextAreaEvaluationStringFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextAreaEvaluationStringFocusGained
        ConditionTextEditorController.createInstance().operationFocusEvaluationGained(
                jTextAreaCondtionText.getText());
    }//GEN-LAST:event_jTextAreaEvaluationStringFocusGained
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnLanguageVariable;
    private javax.swing.JButton btnProfileVariable;
    private javax.swing.JButton jButtonApply;
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JLabel jLabelConditionId;
    private javax.swing.JLabel jLabelConditionText;
    private javax.swing.JLabel jLabelEvaluatedText;
    private javax.swing.JLabel jLabelEvaluationResult;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextAreaCondtionText;
    private javax.swing.JTextArea jTextAreaEvaluationString;
    private javax.swing.JTextField jTextFieldConditionId;
    private javax.swing.JTextField jTextFieldEvaluationResult;
    // End of variables declaration//GEN-END:variables

    @Override
    public void modelPropertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals(
                ConditionTextEditorController.CHANGE_CONDITION_TXT)) {
            jTextAreaCondtionText.setText(evt.getNewValue().toString());
        } else if (evt.getPropertyName().equals(
                ConditionTextEditorController.CHANGE_CONDITION_ID)) {
            jTextFieldConditionId.setText(evt.getNewValue().toString());
            Integer condId = Integer.parseInt(evt.getNewValue().toString());
            jTextFieldConditionId.setVisible(condId > 0);
            jLabelConditionId.setVisible(condId > 0);
        } else if (evt.getPropertyName().equals(
                ConditionTextEditorController.CHANGE_EVALUATED_TXT)) {
            jTextAreaEvaluationString.setText(evt.getNewValue().toString());

        } else if (evt.getPropertyName().equals(
                ConditionTextEditorController.CHANGE_EVALUATION_RESULT_TXT)) {
            jTextFieldEvaluationResult.setText(evt.getNewValue().toString());

        } else if (evt.getPropertyName().equals(
                ConditionTextEditorController.CHANGE_EVALUATION_BOOLEAN)) {
            Boolean evaluatable = (Boolean) evt.getNewValue();
            if(evaluatable){
                jTextAreaEvaluationString.setForeground(Color.BLACK);
                jTextFieldEvaluationResult.setForeground(Color.BLACK);
            }else{
                jTextAreaEvaluationString.setForeground(Color.RED);
                jTextFieldEvaluationResult.setForeground(Color.RED);
            }

        }


    }
}
